/*
 * Placeable.cc
 *
 * @date Mar 14, 2012
 * @author Tim Niggemann, III Phys. Inst. A, RWTH Aachen University
 * @copyright GNU General Public License v3.0
 */

#include "Placeable.hh"

#include <G4PVPlacement.hh>

Placeable::Placeable() : name(""), position(G4ThreeVector(0, 0, 0)), rotation(NULL)
{
	//
}

Placeable::~Placeable()
{
	//
}
// 重构函数一SiPM在随物理体中放置函数
G4VPhysicalVolume *Placeable::buildAndPlace(G4VPhysicalVolume *parent)
{
	return new G4PVPlacement(rotation, position, name + "Pv", build(), parent, false, 0, false);
}
// 重构函数二SiPM在物理体中自定义位置旋转放置函数
G4VPhysicalVolume *Placeable::buildAndPlace(G4VPhysicalVolume *parent, G4ThreeVector _position,
											G4RotationMatrix *_rotation)
{
	setPosition(_position);
	setRotation(_rotation);
	return new G4PVPlacement(rotation, position, name + "Pv", build(), parent, false, 0, false);
}
// 相对于另一物理体放置
G4VPhysicalVolume *Placeable::buildAndPlaceRelativeTo(G4VPhysicalVolume *sibling, G4ThreeVector _position,
													  G4RotationMatrix *_rotation)
{
	// 获取sibling的母体
	G4LogicalVolume *parent = sibling->GetMotherLogical();
	// Transform position and rotation to the parent frame.
	// 设置相对于母体放置位置和旋转，新位置为与sibling位置的叠加，新旋转矩阵为与sibling旋转矩阵相乘
	G4ThreeVector newPosition = _position + sibling->GetObjectTranslation();
	G4RotationMatrix *newRotation = _rotation;
	if (_rotation)
	{
		*newRotation = *(sibling->GetObjectRotation());
	}
	setPosition(newPosition);
	setRotation(newRotation);
	return new G4PVPlacement(newRotation, newPosition, build(), name + "Pv", parent, false, 0, false);
}
// 获取对象名
G4String Placeable::getName() const
{
	return name;
}
// 设置对象名
void Placeable::setName(G4String _name)
{
	name = _name;
}
// 获取位置
G4ThreeVector Placeable::getPosition() const
{
	return position;
}
// 设置位置
void Placeable::setPosition(G4ThreeVector _position)
{
	position = _position;
}
// 获取相对旋转矩阵（相对于sibling）
G4RotationMatrix *Placeable::getRotation() const
{
	return rotation;
}
// 设置相对旋转矩阵（相对于sibling）
void Placeable::setRotation(G4RotationMatrix *_rotation)
{
	if (rotation)
	{
		// 删除旧旋转矩阵避免内存泄漏
		delete rotation;
	}
	rotation = _rotation;
}
